Prevent unnecessary remounts of fragments in 'if' directives - #152
Merged
Merged
Conversation
Merged
berendkleinhaneveld
added a commit
that referenced
this pull request
Jan 27, 2026
This release includes the following features: * Support for template refs (#140) * Hot reloading of components (#146) And the following fixes and improvements: * Prevent unnecessary remounts of elements (#152) * Remove the need for a wrapping template element around dynamic slot content (#147) * Keep elements ordered properly (#155 , #156) * [PySide] Improve interaction with PySide elements (#151) * [PySide] Improve resilience of PySide in light of exceptions (#154)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root Cause
The issue is in ControlFlowFragment.update_fragment() at collagraph/fragment.py:445-454. When the active_child watcher triggers (because a reactive dependency changed), the callback always unmounts and remounts the fragment, even when new is old (same fragment).
The added check in fragments.py prevents the unnecessary unmount/remount cycle when the selected fragment hasn't actually changed.
Why This Happens
The observ watcher with deep=True calls the callback whenever dependencies change (see watcher.py:263: if self.deep or ...), regardless of whether the returned value changed. The fix correctly short-circuits when new is old.